package com.soushin.chinachess.module;

import java.util.LinkedList;

import com.soushin.chinachess.R;
import com.soushin.chinachess.R.layout;
import com.soushin.chinachess.base.BaseActivity;
import com.soushin.chinachess.utils.SharedUtils;
import com.soushin.chinachess.utils.Utils;
import com.soushin.chinachess.widget.GameBoardView;
import com.soushin.chinachess.widget.SelecteDialog;
import com.soushin.chinachess.widget.game.GameConfig;
import com.soushin.chinachess.widget.game.GameLogic;
import com.soushin.chinachess.widget.game.IGameCallback;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
/**
 * 游戏主页
 * @author Administrator
 *
 */
public class MainActivity extends BaseActivity implements IGameCallback, OnClickListener {
	private GameBoardView mGameBoard;//棋盘视图
	private ProgressBar mGameProgress;//加载进度条
	private LinearLayout lltRight;
	private TextView tvReturn, tvRestart;
	private ImageView imgMore;
	private SoundPool mSoundPool;
	private LinkedList<Integer> mSoundList;
	private GameLogic mGameLogic;
	private boolean mSoundEnable;
	private int mHandicapIndex;
	private boolean mComputerFlip;
	private int mPieceStyle;
	private int mAILevel;
	private SelecteDialog sDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initData();
	}

	private void initData() {
		// TODO 数据初始化
		mGameBoard = (GameBoardView) findViewById(R.id.game_board);
		mGameProgress = (ProgressBar) findViewById(R.id.game_progress);
		lltRight = (LinearLayout) findViewById(R.id.llt_right);
		tvReturn = (TextView) findViewById(R.id.tv_return);
		tvRestart = (TextView) findViewById(R.id.tv_restart);
		imgMore = (ImageView) findViewById(R.id.img_more);
		setBack(false);
		setTitle("中国象棋");
		lltRight.setVisibility(View.VISIBLE);
		tvReturn.setOnClickListener(this);
		tvRestart.setOnClickListener(this);
		imgMore.setOnClickListener(this);
		loadDefaultConfig();//加载游戏配置
		initSoundPool();//初始化音效播放器
		initGameLogic();//初始化游戏
	}

	@Override
	protected void onResume() {
		super.onResume();
		//当从设置返回的时候重新加载游戏配置
		loadDefaultConfig();
		mGameLogic.setLevel(mAILevel);
		mGameBoard.setPieceTheme(mPieceStyle);
		mGameBoard.invalidate();
	}

	@Override
	protected void onDestroy() {
		//资源释放  保存游戏配置
		if(sDialog!=null&&sDialog.isShowing()) {
			sDialog.dismiss();
			sDialog=null;
		}
		if (mSoundPool != null) {
			mSoundPool.release();
		}
		SharedUtils.setLast_fen(mGameLogic.getCurrentFen());
		super.onDestroy();
	}

	/**
	 * 加载游戏配置
	 */
	private void loadDefaultConfig() {
		mSoundEnable = SharedUtils.getSwitch_sound()==1?true:false;
		mHandicapIndex = SharedUtils.getHandicap();
		mComputerFlip = SharedUtils.getSwitch_computer_first()==1?true:false;
		mPieceStyle = SharedUtils.getList_chess_style();
		mAILevel = SharedUtils.getComputer_power();
	}

	/**
	 * 初始化音效播放
	 */
	@SuppressLint("NewApi")
	private void initSoundPool() {
		mSoundList = new LinkedList<Integer>();
		int poolSize = GameConfig.SOUND_RES_ARRAY.length;

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			mSoundPool = new SoundPool.Builder().setMaxStreams(poolSize).build();
		} else {
			mSoundPool = new SoundPool(poolSize, AudioManager.STREAM_MUSIC, 0);
		}
		for (int res : GameConfig.SOUND_RES_ARRAY) {
			mSoundList.add(mSoundPool.load(this, res, 1));
		}
	}

	/**
	 * 初始化游戏
	 */
	private void initGameLogic() {
		mGameLogic = mGameBoard.getGameLogic();
		mGameLogic.setCallback(this);//设置下棋回调监听 
		mGameLogic.setLevel(mAILevel);
		mGameBoard.setPieceTheme(mPieceStyle);
		String lastFen = SharedUtils.getLast_fen();
		if (Utils.isEmpty(lastFen)) {
			mGameLogic.restart(mComputerFlip, mHandicapIndex);
		} else {
			showsToasty(getString(R.string.load_last_game_finish));
			mGameLogic.restart(mComputerFlip, lastFen);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO 点击事件监听回调
		switch (v.getId()) {
		case R.id.tv_return:// 悔棋
			  mGameLogic.retract();
			break;
		case R.id.tv_restart:// 重开
		    mGameLogic.restart(mComputerFlip, mHandicapIndex);
            showsToasty(getString(R.string.new_game_started));
			break;
		case R.id.img_more:// 更多
			sDialog=new SelecteDialog(getActivity());
			sDialog.show();
			break;

		default:
			break;
		}
	}

	/**
	 * 播放音效
	 */
	@Override
	public void postPlaySound(int soundIndex) {
		// TODO Auto-generated method stub
		if (mSoundPool != null && mSoundEnable) {
			int soundId = mSoundList.get(soundIndex);
			mSoundPool.play(soundId, 1, 1, 0, 0, 1);
		}
	}

	/**
	 * 消息提示
	 * @param message
	 */
	@Override
	public void postShowMessage(final String message) {
		// TODO Auto-generated method stub
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				showsToasty(message);
			}
		});
	}

	/**
	 * 消息提示
	 * @param messageId
	 */
	@Override
	public void postShowMessage(int messageId) {
		// TODO Auto-generated method stub
		postShowMessage(getString(messageId));
	}

	/**
	 * 显示加载进度条  
	 */
	@Override
	public void postStartThink() {
		// TODO Auto-generated method stub
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mGameProgress.setVisibility(View.VISIBLE);
			}
		});
	}

	/**
	 * 隐藏加载进度条  
	 */
	@Override
	public void postEndThink() {
		// TODO Auto-generated method stub
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mGameProgress.setVisibility(View.GONE);
			}
		});
	}

}
